package com.ssky.bean.validator.util;

import com.ssky.bean.validator.core.rule.Field;
import com.ssky.bean.validator.core.rule.Rule;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

public class DefinerUtils {

    /**
     * 全不为空
     *
     * @param values
     * @return
     */
    public static boolean noneBlank(String... values) {
        if (values == null || values.length == 0) {
            return true;
        }
        for (String value : values) {
            if (StringUtils.isBlank(value)) {
                return false;
            }
        }
        return true;
    }

    /**
     * 至少有一个为null
     *
     * @param values
     * @return
     */
    public static boolean hasNull(String... values) {
        if (values == null) {
            return true;
        }
        for (String value : values) {
            if (value == null) {
                return true;
            }
        }
        return false;
    }

    /**
     * 至少有一个空
     *
     * @param values
     * @return
     */
    public static boolean hasBlank(String... values) {
        if (values == null || values.length == 0) {
            return true;
        }
        for (String value : values) {
            if (StringUtils.isBlank(value)) {
                return true;
            }
        }
        return false;
    }

    public static boolean isCollection(Field field) {
        if (field == null) return false;
        return isCollection(field.getClassType());
    }

    public static boolean isCollection(Class clazzType) {
        if (Collection.class.isAssignableFrom(clazzType))
            return true;
        return false;
    }

    public static boolean isMap(Field field) {
        if (field == null) return false;
        return isMap(field.getClassType());
    }

    public static boolean isMap(Class clazzType) {
        if (Map.class.isAssignableFrom(clazzType))
            return true;
        return false;
    }

    public static boolean isArray(Field field) {
        if (field == null) return false;
        return isArray(field.getClassType());
    }

    public static boolean isArray(Class clazzType) {
        if (clazzType.isArray())
            return true;
        return false;
    }


    public static boolean typeIsIllegal(Field field) {
        if (isMap(field) || isArray(field)) {
            return false;
        }
        return true;
    }

    public static boolean typeIsSupportGet(Class clazzType) {
        if (isCollection(clazzType) || isMap(clazzType) || isArray(clazzType)) {
            return false;
        }
        return true;
    }

    public static boolean typeIsSupportGet(Field field) {
        if (isCollection(field) || isMap(field) || isArray(field)) {
            return false;
        }
        return true;
    }

    public static boolean isJavaClass(Class clazzType) {
        return (clazzType.getClassLoader() == null);
    }


    public static boolean isPrimitive(Class clazzType) {
        return clazzType.isPrimitive();
    }

    public static String getBoxType(Class<?> classType) {
        Class<?> clazz = null;
        if (boolean.class == classType) {
            clazz = Boolean.class;
        } else if (char.class == classType) {
            clazz = Character.class;
        } else if (byte.class == classType) {
            clazz = Byte.class;
        } else if (short.class == classType) {
            clazz = Short.class;
        } else if (int.class == classType) {
            clazz = Integer.class;
        } else if (long.class == classType) {
            clazz = Long.class;
        } else if (double.class == classType) {
            clazz = Double.class;
        } else if (float.class == classType) {
            clazz = Float.class;
        }
        return clazz.getName();
    }

    public static List<Field> toFullFieldList(Field field) {
        List<Field> list = new ArrayList<>();
        list.add(field);
        Field pField = field.getPreField();
        while (pField != null) {
            if (pField.getPreField() != null) {//丢弃第一个，也就是validator校验的bean
                list.add(pField);
            }
            pField = pField.getPreField();
        }
        Collections.reverse(list);
        return list;
    }


    public static boolean isCollectiontElement(Field field) {
        Field pField = field.getPreField();
        while (pField != null) {
            if (DefinerUtils.isCollection(pField)) {
                return true;
            }
            pField = pField.getPreField();
        }
        return false;
    }

    public static boolean isChecker(Rule rule) {
        if (rule.getErrCode() != null && !rule.getErrCode().trim().equals("")) {
            return true;
        }
        return false;
    }

    /**
     * javabean getter规范
     * <li/> boolean--> fly、isFly  -->isFly();
     * <li/> Boolean--> isFly  -->getFly();
     * <li/> Boolean--> fly  -->getFly();
     * <li/> Other Object--> isData ---> getIsDate;
     *
     * @param field
     * @return
     */
    public static String genBeanGetMethodName(Field field) {
        String fieldName = field.getName();
        if (fieldName.length() >= 3) {
            if (Boolean.class.equals(field.getClassType())) {
                if (fieldName.startsWith("is") && isUpperCase(fieldName.charAt(2))) {
                    return "get" + fieldName.substring(2);
                }
            } else if (boolean.class.equals(field.getClassType())) {
                if (fieldName.startsWith("is") && isUpperCase(fieldName.charAt(2))) {
                    return fieldName;
                } else {
                    return "is" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
                }
            }
        }
        return "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
    }

    /**
     * javabean getter规范
     * <li/> boolean--> fly、isFly  -->isFly();
     * <li/> Boolean--> isFly  -->getFly();
     * <li/> Boolean--> fly  -->getFly();
     * <li/> Other Object--> isData ---> getIsDate;
     *
     * @param fieldClass
     * @param fieldName
     * @return
     */
    public static String genBeanGetMethodName(Class fieldClass, String fieldName) {
        if (fieldName.length() >= 3) {
            if (Boolean.class.equals(fieldClass)) {
                if (fieldName.startsWith("is") && isUpperCase(fieldName.charAt(2))) {
                    return "get" + fieldName.substring(2);
                }
            } else if (boolean.class.equals(fieldClass)) {
                if (fieldName.startsWith("is") && isUpperCase(fieldName.charAt(2))) {
                    return fieldName;
                } else {
                    return "is" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
                }
            }
        }
        return "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
    }


    public static String firstCharUpper(String a) {
        if (a == null) {
            return "";
        }
        return a.substring(0, 1).toUpperCase() + a.substring(1);
    }

    /*
     * 是否是大写
     */
    public static boolean isUpperCase(char c) {
        return c >= 65 && c <= 90;
    }

    /*
     * 是否是小写
     */
    public static boolean isLowerCase(char c) {
        return c >= 97 && c <= 122;
    }

    public static void main(String[] args) {
        System.out.println(isPrimitive(Boolean.class));
    }
}
